﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using dokuzeylulworkshop.BaseUtility;
using NHibernate;
using DokuzEylul.Sinav.Entities;
using System.Data.Common;
using System.Collections.Generic;
using DokuzEylul.Sinav.enums;
using DokuzEylul.Sinav.Interfaces;
using System.Diagnostics;

namespace DokuzEylul.Sinav
{
    public partial class deneme : BasePage,ILogin
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            DebugLogin(ConfigurationManager.AppSettings["username"], ConfigurationManager.AppSettings["password"]);
        }
        protected void btnlogin_Click(object sender, EventArgs e)
        {
            if (AuthenticateUser(txtkullaniciAdi.Text, txtsifre.Text))
            {
                LoadUserRoles();
                Response.Redirect("Default.aspx");
                
            }
        }
        [Conditional("DEBUG")]
        private void DebugLogin(string kullaniciAdi, string sifre)
        {
            //developer login
            AuthenticateUser(kullaniciAdi, sifre);

            //developer hakları
            IList<SinavUserRole> _userRoles = m_dbmaster.dbfactory.Select<SinavUserRole>("Select * from SinavUserRole where UserID=?", m_CurrentUser.UserId);

            //sistemde tanımlı haklar
            IList<Role> _existingRoles = m_dbmaster.dbfactory.Select<Role>("Select * from Role");

            //developer hakları ile sistemdeki haklar karşılaştırılıyor eksik olan haklar developer a veriliyor.
            if (_userRoles.Count != _existingRoles.Count)
            {
                bool roleExist = false;
                foreach (Role _role in _existingRoles)
                {
                    roleExist = false;
                    foreach (SinavUserRole _userRole in _userRoles)
                    {
                        if (_userRole.RoleCode == _role.RoleCode)
                            roleExist = true;
                    }
                    if (!roleExist)
                    {
                        DokuzEylul.Sinav.Entities.SinavUserRole sur = new SinavUserRole();
                        sur.MenuId = -1;
                        sur.RoleCode = _role.RoleCode;
                        sur.Userid = m_CurrentUser.UserId;
                        m_dbmaster.dbfactory.Save(sur);
                    }
                }
            }
            LoadUserRoles();
            Response.Redirect("Default.aspx");

        }
        
    }
}
